package com.safeneeds.sysmana.ser.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.safeneeds.sysmana.dto.SysStore;
import com.safeneeds.sysmana.entity.SysUser;
import com.safeneeds.sysmana.entity.SysUserRole;
import com.safeneeds.sysmana.entity.SysUserRoleId;
import com.safeneeds.sysmana.entity.dao.ISysUserDao;
import com.safeneeds.sysmana.entity.dao.ISysUserRoleDao;
import com.safeneeds.sysmana.ser.ISysUserService;
import com.safeneeds.util.base.RetType;
import com.safeneeds.util.other.MD5;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;
import com.safeneeds.util.page.ser.IPageService;

import config.Constant;

/**
 * 系统用户服务类
 * @author gaof
 * @date 2016年7月8日
 */
@Service
@Transactional
public class SysUserService implements ISysUserService {

	@Resource(name="pageService")
	private IPageService pageService;
	
	@Resource(name="sysUserDao")
	private ISysUserDao sysUserDao;
	
	@Resource(name="sysUserRoleDao")
	private ISysUserRoleDao sysUserRoleDao;
	
	/**
	 * 获取系统用户分页数据
	 * @param pageInfo
	 * @return
	 */
	public PageInfo listUserPage(PageInfo pageInfo, SysStore dataStore){
		
		pageInfo = sysUserDao.listUserPage(pageInfo,dataStore);
		pageInfo = pageService.getPageInfoList(pageInfo);
		
		return pageInfo;
		
	}
	
	/**
	 * 删除系统用户(批量)
	 * @param ids
	 * @return
	 */
	public RetType delSysUser(String ids){
		String[] id = ids.split(",");
		
		for (int i = 0; i < id.length; i++) {
			int mid = StringUtil.StringToInt(id[i], 0);
			
			//通过用户id获取用户信息
			SysUser sysUser = sysUserDao.getSysUser(mid);
			if(sysUser == null){
				continue;
			}
			
			//通过用户id删除用户权限
			sysUserRoleDao.delUserRoleByUserId(mid);
			//删除用户
			sysUserDao.delSysUser(sysUser);
		}
		
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		
		return reType;
	}
	
	/**
	 * 通过Id查找系统用户
	 * @param userId
	 * @return
	 */
	public SysUser getUserById(int userId){
		return sysUserDao.getSysUser(userId);
	}
	
	/**
	 * 添加编辑系统用户
	 * @param sysUser
	 * @param roles 
	 */
	public void updateSysUser(SysUser sysUser, String[] roles){
		
		//更新用户
		if (sysUser != null && sysUser.getUserId() != null) {
			SysUser oldUser = sysUserDao.getSysUserById(sysUser.getUserId());
			if(oldUser == null){
				return;
			}
			oldUser.setUserName(sysUser.getUserName());
			//sysUserDao.updateSysUser(sysUser2);
			
			updateUserRole(oldUser.getUserId(), roles);
			
		//添加用户
		}else {
			sysUser.setPwd(MD5.toMD5("123456".getBytes()));//默认密码为123456
			sysUser.setStatus(Constant.SYS_USER_STATUS_NORMAL);//用户状态
			sysUser = sysUserDao.addSysUser(sysUser);
			
			updateUserRole(sysUser.getUserId(), roles);
		}
		
		
	}
	
	/**
	 * 更新用户权限
	 * @param userId
	 * @param roles
	 */
	private void updateUserRole(Integer userId, String[] roles) {
		
		List<SysUserRole> userRoles = new ArrayList<SysUserRole>();
		for(String role : roles){
			int roleId = StringUtil.StringToInt(role, 0);
			if(roleId <= 0){
				continue;
			}
			
			SysUserRole userRole = new SysUserRole();
			SysUserRoleId userRoleId = new SysUserRoleId();
			userRoleId.setRoleId(roleId);
			userRoleId.setUserId(userId);
			userRole.setId(userRoleId);
			
			userRoles.add(userRole);
		}
		
		//删除用户原有权限
		sysUserRoleDao.delUserRoleByUserId(userId);
		//添加用户权限
		sysUserRoleDao.addUserRoles(userRoles);
	}

	/**
	 * 重置系统用户密码
	 * @param id
	 */
	public void resetPWD(int id){
		SysUser sysUser = sysUserDao.getSysUser(id);
		sysUser.setPwd(MD5.toMD5("123456".getBytes()));
		sysUserDao.updateSysUser(sysUser);
	}
	
	/**
	 * 修改密码
	 * @param userId
	 * @param newPWD2
	 */
	public void editPWD(Integer userId, String newPWD2){
		SysUser sysUser = sysUserDao.getSysUser(userId);
		sysUser.setPwd(MD5.toMD5(newPWD2.getBytes()));
		sysUserDao.updateSysUser(sysUser);
	}

	/**
	 * 通过用户id获取用户角色
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysUserRole> listUserRolesByUserId(int userId) {
		return sysUserRoleDao.getUserRolesByUserId(userId);
	}
}
